#! /usr/bin/env python
# coding=utf-8
import json,sys,time,random,string
from live_api import pili
from constant import datais
import urllib2,urllib
import base64


access_key = datais.access_key
secret_key = datais.secret_key
hub_name = datais.bucket_name_live
dns_live=datais.dns_live
def api_createRoom(info):
    if access_key == "" or secret_key == "":
        # print "need set access_key and secret_key"
        sys.exit(1)
    mac = pili.Mac(access_key, secret_key)
    streamtitle='liuhua%d%s'%(time.time(),''.join(random.sample(string.ascii_letters, 1)))
    info.rtmp_publish_url=pili.rtmp_publish_url("pili-publish.%s" % dns_live, hub_name, streamtitle, mac, 60)
    # print "RTMP play URL:"
    info.rtmp_play_url= pili.rtmp_play_url("pili-live-rtmp.%s" % dns_live, hub_name, streamtitle)
    # info.rtmp_play_url= pili.hls_play_url("pili-live-rtmp.%s" % dns_live, hub_name, streamtitle)
    info.hls_play_url= pili.hls_play_url("pili-live-rtmp.%s" % dns_live, hub_name, streamtitle)
    return info

def api_saveas(info):
    mac = pili.Mac(access_key, secret_key)
    # print 11111111111,hub_name,info.key
    stream=pili.Stream(mac.__auth__, hub=hub_name, key=info.key)
    res = stream.saveas()
    return res
